package gui;

import controller.FilesController;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

public class FilesView extends JPanel {

    private DefaultListModel includedFiles = new DefaultListModel();
    private DefaultListModel excludedFiles = new DefaultListModel();
    private JList includedList = new JList(includedFiles);
    private JList excludedList = new JList(excludedFiles);

    public FilesView() {
        setLayout(new GridBagLayout());

        GridBagConstraints c = new GridBagConstraints();

        c.gridx = 0;
        c.gridy = 0;
        c.weightx = 0.4;
        c.weighty = 0.9;
        c.fill = GridBagConstraints.BOTH;
        
        add(new JScrollPane(includedList), c);
        c.gridx = 2;
        add(new JScrollPane(excludedList), c);
        Box verticalBox = Box.createVerticalBox();
        JButton excludeButton = new JButton(">>");
        excludeButton.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent ae) {
                FilesController.getInstance().shiftFiles(includedFiles, excludedFiles, includedList.getSelectedValues());
            }
        });

        JButton includeButton = new JButton("<<");
        includeButton.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent ae) {
                FilesController.getInstance().shiftFiles(excludedFiles, includedFiles, excludedList.getSelectedValues());
            }
        });
        
        
        
        c.gridx = 1;
        c.weightx = 0.1;
        
        JPanel buttonsPanel = new JPanel();
        buttonsPanel.setLayout(new BoxLayout(buttonsPanel, BoxLayout.Y_AXIS));
        buttonsPanel.add(Box.createVerticalGlue());
        excludeButton.setAlignmentX(Component.CENTER_ALIGNMENT);
        buttonsPanel.add(excludeButton);
        includeButton.setAlignmentX(Component.CENTER_ALIGNMENT);
        buttonsPanel.add(includeButton);
        buttonsPanel.add(Box.createVerticalGlue());

        add(buttonsPanel, c);

        JButton commitButton = new JButton("Commit");
        commitButton.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent ae) {
                FilesController.getInstance().commit();
            }
        });
        c.gridx = 0;
        c.gridy = 1;
        c.weighty = 0.1;
        c.fill = GridBagConstraints.NONE;
        c.gridwidth = 3;
        add(commitButton, c);
    }

    public DefaultListModel getIncludedFilesModel() {
        return includedFiles;
    }

    public DefaultListModel getExcludedFilesModel() {
        return excludedFiles;
    }
    
    public JList getIncludedList(){
        return includedList;
    }
    
    public JList getExcludedList(){
        return excludedList;
    }
}
